﻿<%@ Page Language="C#" MasterPageFile="~/Pages/Base/ListItemBase.master" CodeFile="~/Pages/Base/ListItemBase.cs" Inherits="ListItemBasePage" %>
<script runat="server">
    
    /// <summary>
    /// 数据库交互对象
    /// </summary>

    private StateGrid.NOTICE.Notice_DAO dao = new StateGrid.NOTICE.Notice_DAO();

    private IList<StateGrid.NOTICE.Notice> data;


    protected override void DoInit()
    {        
    }

    protected override void SetItemFields()
    {
        RegisterKeyField("id");
        AddBoudField("标题", "title", 0, 65);
        AddBoudField("发布时间", "submit_time", 0,20);
        AddHyperLinkField("查看",
                 StateGrid.GlobalVariables.GlobalNOTICE.EDITPM_PAGE + "?id={0}&type=visit",
                 new string[] { "id" }, "_blank");
        StateGrid.User.User user = (StateGrid.User.User)Session["login"];
        if (null == user)
        {
            return;
        }
        if (user.role == StateGrid.User.UserRole.ADMIN)
        {
            AddHyperLinkField("修改",
                         StateGrid.GlobalVariables.GlobalNOTICE.EDITPM_PAGE + "?id={0}&type=edit",
                         new string[] { "id" }); 
        } 
    }

    protected override int GetItemCount()
    {
        return dao.CountNOTICE();
    }

    protected override object GetItemSource(int begin, int end)
    {
        data = dao.SelectSomeNOTICE( begin, end);
        if (null == data)
        {
            return null;
        }
        foreach (StateGrid.NOTICE.Notice temp in data)
        {
            int length = temp.title.Length;
            if (length > 50)
            {
                temp.title = temp.title.Substring(0, 50) + "……";
            }
        }
        return data;
    }

    protected override bool OnDelete(DataKey dataKey, out string message)
    {
        int id = 0;
        try
        {
            id = Int32.Parse(dataKey.Value.ToString());
        }
        catch (Exception)
        {
            id = -1;
        }

        if (-1 != id)
        {
            Boolean flag = dao.DeleteNOTICEByID(id);

            if (flag)
            {
                message = "删除成功";
                return true;
            }

        }
        
        message = "删除失败";
        return false;
    }


    //显示删除按钮
    protected override Boolean HasDeleteField()
    {
        StateGrid.User.User user = (StateGrid.User.User)Session["login"];
        if (null == user)
        {
            return false;
        }

        if (user.role == StateGrid.User.UserRole.ADMIN)
        {
            return true;
        }
        return false;
    }
</script>
